import { NextResponse } from "next/server"; import automation from "data/automation.json"; const get = (url) => { return fetch(url, { cache: "no-store" }) .then((resp) => resp && resp.text()) .then((result) => result); }; const update = ({ id, client, ...rest }) => { let url = `http://${client}/win`; if (id) url += `&PL=${id}`; return get(url) .then((resp) => ({ ps: id })) .catch((err) => ({ error: err.message })); }; export async function GET(req, { params }) { let promises = []; let id = params?.id || null; try { if (!id) throw new Error("No id specified"); let clients = (automation?.[1] && Object.keys(automation?.[1])) || []; if (!clients) throw new Error("No clients for id", id); for (let client of clients) { promises.push(update({ id, client })); } } catch (err) { return NextResponse.json({ error: err?.message }, { status: 500 }); } return Promise.allSettled(promises) .then((results) => { return NextResponse.json(results?.map((o) => o?.value)); }) .catch((err) => { return NextResponse.json({ error: err?.message }, { status: 500 }); }); }